var express = require('express');
var router = express.Router();
var fs  = require("fs");

var multer  = require('multer')
var uploads_dir= 'public/uploads/'
var upload = multer({ dest: uploads_dir })
var conn = require('../conn');

// router.use(function (req, res, next) {
// 	if (req.session.password == true) {
// 			next();
// 	}else{
// 			res.redirect("/users/login")
// 	}
// });
router.get('/', function(req, res, next) {
	var sql='select count(1) as c from articles';
	conn.query(sql, function(err, rows, fields) {
		req.totolpage = rows[0].c;
		next();
	})	
},function(req, res, next) {
	var page = req.query.page;
	var pagenum = '10';
	var totolpage =Math.ceil(req.totolpage/pagenum) 
	var sql='select *,articles.id as aid from articles join catalog on articles.cata_id=catalog.id order by aid desc limit ';
	if (page == undefined) {
		page=1
	}
	// if (req.url!="/") {
		sql = sql+(page-1)*pagenum+','+pagenum;
	// }else{
	// 	res.redirect('?page=1');
	// 	sql = sql+pagenum;	
	// }
	conn.query(sql, function(err, rows, fields) {
		console.log(rows)
		if (err) throw err;
	res.render('articlelist',{articles:rows,totolpage:totolpage,username:req.session.username,currentpage:page,success:req.flash('success')});	
	})	
												
}); 
router.get('/text',function(req,res,next){
	res.send("123")
})
router.get('/addArticle', function(req, res, next) {
	var sql = 'select * from catalog'
	conn.query(sql, function(err, rows, fields) {
		if (err) throw err;
		res.render('addArticle',{title:"添加文章",catalogs:rows,error:req.flash("error")});	
	})																	
});
router.post('/addArticle',function(req, res, next) {
	console.log("没找到"+req.body.name)
	if (req.body.title=="") {
		req.flash('error', '标题不能为空!')
		res.redirect('addArticle');
	}else if (req.body.img=="") {
		req.flash('error', '图片不能为空!')
		res.redirect('addArticle');
	}else if (req.body.cata_id=="$") {
		req.flash('error', '请选择分类!')
		res.redirect('addArticle');
	}else  if (req.body.content=="") {
		req.flash('error', '内容不能为空!')
		res.redirect('addArticle');
	}else{

		var sql = 'insert into articles values(null,"'+req.body.title+'","'+req.body.content+'","'+req.body.img+'","'+req.body.cata_id+'")'
		conn.query(sql, function(err, rows, fields) {
			req.flash('success', '添加文章成功！')
	  		res.redirect('/admin');
			
	  	});

	}
}); 

router.get('/del', function(req, res, next) {
	var sql = 'delete from articles where articles.id='+req.query.id
	conn.query(sql, function(err, rows, fields) {
			    if (err) throw err;
  		res.redirect('/admin')
  	})															
}); 

router.get('/editArticle', function(req, res, next) {
	var sql = 'select * from catalog'
	conn.query(sql, function(err, rows, fields) {
		catalogs = rows;
		sql = "select * from articles where id="+req.query.id;
		conn.query(sql, function(err, rows, fields) {
			res.render('editArticle',{title:"添加文章",catalogs:catalogs,articles:rows[0]});	
		})
	})																	
});
router.post('/editArticle',function(req, res, next) {
	var sql = 'update articles set title="'+req.body.title+'",content="'+req.body.content+'",img="'+req.body.img+'",cata_id="'+req.body.cata_id+'" where id='+req.body.id
	conn.query(sql, function(err, rows, fields) {
		res.redirect('/admin')
	})																	
});
router.get('/outlogin',function(req,res,next){
	req.session.destroy();
	res.redirect('/users/login')
})
router.post('/uploadfile',upload.single('fileInput'),function(req, res, next) {
	fs.renameSync(uploads_dir+req.file.filename,uploads_dir+req.file.originalname)
	var img = 'uploads/'+req.file.originalname
	res.send(img)														
});
module.exports = router;
